package system

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"new_club_recruitment/global"
	"new_club_recruitment/model/common/request"
	"new_club_recruitment/model/common/response"
	"time"
)

type RedEnvelope struct{}

// RedEnvelopes 发红包
func (RedEnvelope *RedEnvelope) RedEnvelopes(c *gin.Context) {
	var info request.RedEnvelopeInfo
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	info.Time = time.Duration(info.Hour) * time.Hour
	//2.处理业务
	if err := redisSerice.CreateRedEnvelope(&info); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(info.Id, "创建成功", c)
	}
}

// GrabARedEnvelope 抢红包
func (RedEnvelope *RedEnvelope) GrabARedEnvelope(c *gin.Context) {
	var info request.RedEnvelopeGrabber
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if money, err := redisSerice.GetRedEnvelope(&info); err != nil {
		global.GVA_LOG.Error("抢夺失败!", zap.Error(err))
		response.FailWithMessage("抢夺失败!", c)
	} else {
		//3.返回响应
		response.OkWithDetailed(money, "抢夺成功", c)
	}
}

// RedPacketRcptSts 红包领取情况
func (RedEnvelope *RedEnvelope) RedPacketRcptSts(c *gin.Context) {
	var info request.RedEnvelopeGrabber
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if res, err := redisSerice.RedEnvelopeRes(info.Key); err != nil {
		global.GVA_LOG.Error("已过期!", zap.Error(err))
		response.FailWithMessage("已过期!", c)
	} else {
		//3.返回响应
		response.OkWithDetailed(res, "查找成功", c)
	}
}
